<template>
  <div class="department-manage-container">
    <el-date-picker
        v-model="selectedDate"
        type="month"
        placeholder="选择月份"
        format="YYYY-MM"
        value-format="YYYY-MM"
        @change="handleMonthChange"
        style="margin-bottom: 20px"
    ></el-date-picker>
    <el-table
        v-loading="departmentStore.loading"
        :data="departmentStore.departmentList"
        element-loading-text="加载中..."
        border
        default-expand-all
        row-key="selfid"
        :tree-props="{ children: 'childs', hasChildren: 'hasChildren' }"
    >
      <StoreNameColumn />
      <el-table-column
          prop="type"
          label="公司类型"
          width="120"
      >
        <template #default="{ row }">
          {{ getOrgType(row) }}
        </template>
      </el-table-column>
      <el-table-column
          prop="memberCount"
          label="员工数"
          width="100"
      >
        <template #default="{ row }">
          {{ row.memberCount || '--' }}
        </template>
      </el-table-column>
      <el-table-column
          prop="manager"
          label="负责人"
          width="120"
      >
        <template #default="{ row }">
          {{ row.manager || '--' }}
        </template>
      </el-table-column>
      <el-table-column
          prop="leader"
          label="分管领导"
          width="120"
      >
        <template #default="{ row }">
          {{ row.leader || '--' }}
        </template>
      </el-table-column>
      <el-table-column
          label="是否完成业绩"
          width="200"
          fixed="right"
      >
        <template #default="{ row }">
          <el-radio-group
              v-model="row.iscomplete"
              @change="showConfirmDialog(row)"
          >
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
    </el-table>

  </div>

</template>

<script setup lang="ts">
import {ref, onMounted, nextTick} from 'vue'
import { useStore } from '@/stores/performance'
import type { DepartmentItem } from '@/api/types/department'
import DepartmentDialog from '@/components/DepartmentDialog.vue'
import { WarningFilled } from '@element-plus/icons-vue'
import StoreNameColumn from "@/components/StoreNameColumn.vue";
import { ElMessage } from 'element-plus'

const departmentStore = useStore()

const selectedDate = ref<string>('')
const selectedRow = ref(<Object>({
  shop_id: 0,
  recordmonth: '',
  iscomplete: 0
}))

// 获取组织类型
const getOrgType = (row: DepartmentItem) => {
  if (row.pid === -1) return '公司'
  if (row.pid === 0) return '公司'
  return '--'
}

// 初始化数据
onMounted(() => {
  setDefaultMonth()
  departmentStore.fetchDepartmentList(selectedDate.value)
})
// 设置默认月份为当前月份
const setDefaultMonth = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  selectedDate.value = `${year}-${month}-01`;
};
// 处理月份选择变化
const handleMonthChange = (value: string) => {
  if (value) {
    selectedDate.value = `${value}-01`;
    fetchData();
  }
};
//选择日期触发
const fetchData = async () => {
  if (!selectedDate.value) {
    ElMessage.warning('请选择日期');
    return;
  }

  try {
    const response = departmentStore.fetchDepartmentList(selectedDate.value)
      ElMessage.success('查询成功')
  } catch (error) {
    console.error('Error fetching data:', error);
    ElMessage.error('查询失败');
  }
};
// 是否完成绩效更改
const showConfirmDialog = async (row: object) => {
  selectedRow.value.shop_id = row.selfid;
  selectedRow.value.recordmonth = selectedDate.value;
  selectedRow.value.iscomplete = row.iscomplete;
  const response = await departmentStore.updateDepartment(selectedRow.value);
  setDefaultMonth();
};
</script>

<style lang="scss" scoped>
.department-manage-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;

  :deep(.el-table__row) {
    .cell {
      white-space: nowrap;
    }
  }
}

.delete-dialog {
  :deep(.el-dialog__header) {
    margin-right: 0;
    padding: 20px 24px;
    border-bottom: 1px solid #f0f0f0;

    .el-dialog__title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
  }

  :deep(.el-dialog__body) {
    padding: 24px;
  }

  :deep(.el-dialog__footer) {
    padding: 16px 24px;
    border-top: 1px solid #f0f0f0;
  }

  .delete-content {
    display: flex;
    align-items: flex-start;
    gap: 16px;

    .warning-icon {
      padding-top: 4px;
    }

    .delete-text {
      flex: 1;

      .warning-title {
        font-size: 16px;
        font-weight: 500;
        color: #303133;
        margin-bottom: 16px;
      }

      .delete-info {
        background-color: #fafafa;
        border-radius: 4px;
        padding: 12px 16px;
        margin-bottom: 12px;

        .info-item {
          line-height: 24px;
          display: flex;
          align-items: center;

          .label {
            color: #606266;
            margin-right: 8px;
            min-width: 70px;
          }

          .value {
            color: #303133;
            font-weight: 500;
          }
        }
      }

      .warning-desc {
        color: #ff4d4f;
        font-size: 12px;
      }
    }
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;

    .el-button {
      padding: 8px 20px;
      min-width: 88px;
    }
  }
}
</style>
